# -*- coding=utf-8 -*-
from flask import request, Blueprint
from jeepay.utils import Utils

notice = Blueprint("notice", __name__)


@notice.route("/content", methods=["POST"])
def notice_content():
    return verify_signature()


def verify_signature():
    try:
        get_params, get_sign = analysis_get_params()
    except TypeError:
        return "FAIL"
    else:
        signature = Utils._signature_rules(get_params)
        if signature == get_sign:
            print(
                "验签成功\n"
                f"获取sign-{get_sign}\n"
                f"生成sign-{signature}"
            )
            return "SUCCESS"
        else:
            print(
                "验签失败\n"
                f"获取sign-{get_sign}\n"
                f"生成sign-{signature}"
            )
            return "FAIL"


def analysis_get_params():
    new_params = {}
    get_params = request.form.to_dict()
    print(f"通知接收到的参数-{get_params}")
    if get_params:
        get_sign = get_params["sign"]
        for key, value in get_params.items():
            if key != "sign" and value or value == 0:
                new_params[key] = value
        return new_params, get_sign
    else:
        return None
